var base = require('conf.js')
var request = new Object();






request.wxRequest = function(obj){
  var code = wx.getStorageSync('access_token')
  wx.request({
    url:base.url+obj.url+'&wechat_id='+base.wechat_id+'code='+code,
    data:obj.data,
    method:obj.method,
    header:{"Content-Type": "application/x-www-form-urlencoded"},
    success:function(res){
      //console.log(JSON.stringify(res));
      if(res.statusCode ==200 &&res.data.code == 'success'){
        typeof(obj.success)=='function' && obj.success(res.data.data);
      }
    }
  })

}










/**
 * 登录时序图
 * 1.wx.login取得code
 * 2.根据code请求微信服务器获取openId
 * https://api.weixin.qq.com/sns/jscode2session?appid=wx9d06f3bf444bd59d&secret=SECRET&js_code=013hG1zj0tR2Kk1SZfzj0zA7zj0hG1zf&grant_type=authorization_code
 * 3.session为key,session_key+openId为value,写入session存储, session存入storage.wx.setStorageSync
 * 4.wx.checkSession 验证用户登陆态
 * wx.checkSession({
  success: function(){
    //session 未过期，并且在本生命周期一直有效
  },
  fail: function(){
    //登录态过期
    wx.login() //重新登录
    ....
  }
})
 */



request.register = function(){

  //1.微信登录
  wx.login({
   success:function(res){
    if(res.code){
      var code = res.code
     //  console.log("--------------code:"+JSON.stringify(code));
       //2.微信获取用户信息


//2.通过code获取openId
 wx.request({  
      url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx9d06f3bf444bd59d&secret=b02beab63fb3afdc3e0c13f4900f0cf6&grant_type=authorization_code&js_code='+code,  
      header: {  
          'content-type': 'application/json'  
      },  
      success: function(res) {  
     // console.log("----------------openId:"+JSON.stringify(res)); //获取openid  

          var access_token = wx.getStorageSync('access_token');
          var authData = {
            'authData':
            {'weapp':
              {
               'id':  res.data.openid,
              
              }
            }
          }
       //    console.log("--------------authData:"+JSON.stringify(authData));

//3.登录后台
            wx.request({
            url:'http://core-dev.dzxwapp.com/parse/users',
            data:authData,
            method:"POST",
            header:{
              'Content-Type': ' application/json',
              'X-Parse-Application-Id': 'jiKapGuqdTzWg62WgkDMKNUo',
              'X-Parse-Revocable-Session': '1' 
            },
            success:function(res){
                        console.log("--------------loginres:"+JSON.stringify(res));

              if(res.statusCode ==200){
                try {
                  wx.setStorageSync('current_userId', res.data.objectId)
                  wx.setStorageSync('access_token', res.data.sessionToken)
                } catch (e) {
                  console.log(e)  
                }
              }else{
                console.log(res)
              }
            }
          })


      }  
    }) 


    }
   }
  })
}




request.wxRequestForDZXW = function (obj) {
  var code = wx.getStorageSync('access_token');
  var url = base.urlForDZXW + obj.url;
  //var url = base.url + obj.url + '&wechat_id=' + base.wechat_id + 'code=' + code
   console.log('url:'+url)
var sessionToken =  wx.getStorageSync('access_token');
console.log('sessionToken:'+sessionToken)

  wx.request({
    url: url,
    data: obj.data,
    method: obj.method,
    header: { 
      'X-Parse-Application-Id': 'jiKapGuqdTzWg62WgkDMKNUo',
      'content-type': 'application/json',
    'X-Parse-Session-Token': sessionToken
     },
    success: function (res) {
      //console.log(JSON.stringify(res));
        if(res.statusCode ==200)
        {
         obj.success(res);
       }
      }
  })

}

module.exports = request